package com.ascendere9.ff.pof.util;

import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * @author: ThanhNH
 */
public class CommonUtil {

	// ===================================================================
	// Constants
	// ===================================================================
	private static final String LOG_TAG = CommonUtil.class.getSimpleName();

	// ===================================================================
	// Fields
	// ===================================================================

	// ===================================================================
	// Constructors
	// ===================================================================

	// ===================================================================
	// Getter & Setter
	// ===================================================================

	public static LayoutInflater getLayoutInflater(Context pContext) {
		return (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public static void showSoftKeyboard(Context pContext) {
		InputMethodManager imm = (InputMethodManager) pContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}

	public static void showSoftKeyboard(Window pWindow) {
		pWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
	}

	@Deprecated
	public static void hideSoftKeyboard(Activity pActivity) {
		InputMethodManager inputMethodManager = (InputMethodManager) pActivity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		View viewFocus = pActivity.getCurrentFocus();
		if (viewFocus != null) {
			inputMethodManager.hideSoftInputFromWindow(viewFocus.getWindowToken(), 0);
		}
		pActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	public static void hideSoftKeyboard(View pView) {
		InputMethodManager imm = (InputMethodManager) pView.getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(pView.getWindowToken(), 0);
	}

	public static int countTreeView(int pCount, View pView) {
		pCount += 1;
		if (pView instanceof ViewGroup) {
			ViewGroup viewGroup = (ViewGroup) pView;
			for (int i = 0; i < viewGroup.getChildCount(); i++) {
				View view = viewGroup.getChildAt(i);
				pCount = countTreeView(pCount, view);
			}
		}
		return pCount;
	}

	public static int maxDeepTreeView(int pDeep, View pView) {
		pDeep++;
		int maxDeep = pDeep;
		if (pView instanceof ViewGroup) {
			ViewGroup viewGroup = (ViewGroup) pView;
			for (int i = 0; i < viewGroup.getChildCount(); i++) {
				View view = viewGroup.getChildAt(i);
				int newDeep = maxDeepTreeView(pDeep, view);
				if (maxDeep < newDeep) {
					maxDeep = newDeep;
				}
			}
		}
		return maxDeep;
	}

	public static String getFullDeviceId(Context pContext) {
		TelephonyManager tManager = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
		final String tmDevice, tmSerial, androidId;
		tmDevice = "" + tManager.getDeviceId();
		tmSerial = "" + tManager.getSimSerialNumber();
		androidId = ""
				+ android.provider.Settings.Secure.getString(pContext.getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);

		UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32)
				| tmSerial.hashCode());
		String deviceId = deviceUuid.toString();
		return deviceId;
	}

	public static String getDeviceId(Context pContext) {
		TelephonyManager tManager = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tManager.getDeviceId();
	}

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================

	// ===================================================================
	// Methods
	// ===================================================================

	// ===================================================================
	// Methods for SubClass
	// ===================================================================

	// ===================================================================
	// Inner and Anonymous Classes
	// ===================================================================
}
